package com.baizhi.service.impl;

import com.baizhi.dao.CmfzUserDao;
import com.baizhi.service.CmfzUserService;
import com.baizhi.util.ThreadPollUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

@Service
public class CmfzUserServiceImpl implements CmfzUserService {
    /**
     * Logger LoggerFactory 都是org.slf4j包的
     * 参数是当前类的类对象
     */
    private Logger logger = LoggerFactory.getLogger(CmfzUserServiceImpl.class);

    @Autowired
    private CmfzUserDao cmfzUserDao;


    @Override
    public List<Map> selectBySexCount() {
        return cmfzUserDao.selectBySexCount();
    }

    @Override
    public List<Map> selectByProvince() {
        return cmfzUserDao.selectByProvince();
    }

    @Override
    public Map selectByDayCount() {
        Map map = new HashMap();
        map.put("one",cmfzUserDao.selectByDayCount(7,1));
        map.put("two",cmfzUserDao.selectByDayCount(14,7));
        map.put("three",cmfzUserDao.selectByDayCount(21,14));


        return map;
    }

    @Override
    public Map selectByThread() throws InterruptedException {
//        1.创建三个子线程 初始化计数器
        Map map = new HashMap();
        CountDownLatch countDownLatch = new CountDownLatch(3);

        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
//                注册量
                Map map1 = selectByDayCount();
                map.put("count",map1);

//                计数器减一
                countDownLatch.countDown();
            }
        });
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
//                男女人数
                List<Map> maps1 = selectBySexCount();
                map.put("sex",maps1);
//                计数器减一
                countDownLatch.countDown();
            }
        });
        ThreadPollUtil.executor.submit(new Runnable() {
            @Override
            public void run() {
//                地区分布
                List<Map> maps = selectByProvince();
                map.put("province",maps);
//                计数器减一
                countDownLatch.countDown();
            }
        });

        /**
         * 通过await方法让主线程等待
         */
        countDownLatch.await();

        return map;
    }
}
